cmake_minimum_required(VERSION 3.21.0)

# Get folder name and set as project name
get_filename_component(ProjectName ${CMAKE_CURRENT_SOURCE_DIR} NAME)
project(${ProjectName} VERSION 1.0 LANGUAGES C CXX)

message("Project name: ${PROJECT_NAME}")

# Set C++ standards
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_STANDARD_REQUIRED ON)

# Compile definitions
add_compile_definitions(U8G2_USE_LARGE_FONTS)
add_compile_definitions(ARDUINO)

# Source files
file(GLOB MainFiles CONFIGURE_DEPENDS "src/*.cpp")
file(GLOB U8g2SDL CONFIGURE_DEPENDS "lib/u8g2/sys/sdl/common/*.c")
file(GLOB BetweenFaceFiles CONFIGURE_DEPENDS "lib/between_face/src/*.cpp")
file(GLOB ArduinoFiles CONFIGURE_DEPENDS "lib/arduino/*.cpp")
file(GLOB ArduinoCFiles CONFIGURE_DEPENDS "lib/arduino/*.c")
file(GLOB ArduinoAVRFiles CONFIGURE_DEPENDS "lib/arduino/avr/*.c")

# Create executable
add_executable(${PROJECT_NAME} 
    ${MainFiles}
    ${U8g2SDL}
    ${BetweenFaceFiles}
    ${ArduinoFiles}
    ${ArduinoCFiles}
    ${ArduinoAVRFiles}
)

target_include_directories(${PROJECT_NAME} PRIVATE src)
target_include_directories(${PROJECT_NAME} PRIVATE lib/between_face/include)
target_include_directories(${PROJECT_NAME} PRIVATE lib/arduino)
target_include_directories(${PROJECT_NAME} PRIVATE lib/arduino/avr)

# U8G2 Library
add_subdirectory(lib/u8g2)
target_link_libraries(${PROJECT_NAME} PRIVATE u8g2)
target_compile_definitions(u8g2 PRIVATE ARDUINO)

# SDL2 Configuration
set(SDL2_DIR "lib/SDL2/cmake")
find_package(SDL2 REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE ${SDL2_LIBRARIES})

# copy DLL to build directory（Windows）
if(WIN32)
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy
            ${CMAKE_SOURCE_DIR}/lib/SDL2/x86_64-w64-mingw32/bin/SDL2.dll
            $<TARGET_FILE_DIR:${PROJECT_NAME}>
    )
endif()